import java.util.ArrayList;

public class Demo1 {
    /**
     * 和为s的两个数->牛客网
     *      1.取left right,判断和是否等于s
     *      2.根据判断结果进行移动left right,直到找到到两个数
     */
    public static ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
        int left = 0;
        int right = array.length - 1;
        ArrayList<Integer> arrayList = new ArrayList<>();
        while(left < right) {
            if(array[left] + array[right] == sum) {
                arrayList.add(array[left]);
                arrayList.add(array[right]);
                return arrayList;
            }else if(array[left] + array[right] > sum) {
                right--;
            }else {
                left++;
            }
        }
        return arrayList;
    }
    public static void main(String[] args) {
        int[] array = {1,2,2,4};
        int sum = 4;
        System.out.println(FindNumbersWithSum(array,sum));
    }
}
